<?php
namespace Common\Model;

/**
 * Model
 */
class Model extends \Think\Model {
    public function save2($d) {
        if (empty($d)) {
            throw new \Exception("请指定需保存数据");
        }

        try {
            $d = $this->create($d);
        } catch(\Exception $e) {
            throw new \Exception($e->getMessage());
        }

        $action = empty($d[$this->pk]) ? 'add' : 'save';

        return $this->$action($d);
    }
	public function searchTime($d){
		if(empty(trim($d['date1'])) && empty(trim($d['date2']))){
			return;
		}
		echo 'asd';
		if(empty(trim($d['date1']))){
			$d['date1'] = '1970-01-01';
		}
		if(empty(trim($d['date2']))){
			$d['date2'] = '2035-12-31';
		}	
		$expr['create_time'] = array('between' , $d['date1'] .','.$d['date2']);
		return $expr;
	}
	
    public function page2($param = array()) {
        $size     = empty($param['size'])     ? 20 : (int)$param['size'];
        $sideSize = empty($param['sideSize']) ? 5  : (int)$param['sideSize'];
        $urlParam = empty($param['param'])    ? '' : $param['param'];
        $total    = empty($param['total'])    ? 0  : (int)$param['total'];
        $options       = $this->_parseOptions($this->options);
		
        $this->options = $options;  //_parseOptions()会清空$this->options
        /**
         * 加入默认按主键倒序
         */
        if(empty($options['order'])) {
            $options['order'] = $this->getPk() . ' DESC';
        }
		
        /**
         * 如果COUNT()时有order by在PostgreSQL中会出错 
         */
        if(!empty($this->options['order'])) {
            unset($this->options['order']);
        }
        if(empty($total)){
            if(empty($this->options['group'])) {
                $total = $this->getField('COUNT(*) AS tp_count');
            } else {
                $subQuery = $this->field('count(*)')->where($this->options['where'])->group($this->options['group'])->select(false);
                $total    = $this->table($subQuery . ' sub')->Count();
            }

            if(empty($total)) {
                return false;
            }

            $page               = new Page($total, $size, $sideSize, $urlParam);
            $pageInfo           = $page->fetch();
            $options['limit']   = "{$pageInfo['start']},{$pageInfo['size']}";
            $options            = $this->_parseOptions($options);
            $pageInfo['record'] = $this->db->select($options);
            return $pageInfo;
        }
    }

    public function del($id) {
        $id = is_array($id) ? $id : (int)$id;

        if(empty($id)) {
            throw new \Exception('参数不完整，无法删除数据');
        }

        $expr = is_array($id) 
                ? ['IN', $id] 
                : $id;

        if (!$this->where([$this->pk => $expr])->delete()) {
            return false;
        }

        return true;
    }


    /**
     * parseOptsField
     * 返回分解后的值
     * 处理方法是返回第一级对应的值
     *
     * @param mixed $d  表中opts字段值
     * @return void
     */
    public function parseOptsField($opts) {
        if (!$a = json_decode($opts, true)) {
            return false;
        }

        $r = [];

        foreach ($a as $k => $v) {
            if (is_array($v)) {
                $r = array_merge($r, $v);
            } else {
                $r[$k] = $v;
            }
        }

        return $r;
    }

}
